<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/25 0025
 * Time: 15:15
 */

namespace app\admin\controller;


use think\Request;

class Nav extends Base
{
    protected $_Nav;
    public function initialize(){
        $this -> _initialize();
        $this->_Nav = new \app\model\model\Nav();
    }
    public function lists(){
        $data = $this->_Nav->getList();

        $nav = [];
        $i = 1;
        foreach ($data as $k=>$v){
            if($v['type']==0){
                $v['name'] = "".$v['name'];
                $nav[$i] = $v;
                $i++;
               foreach ($data as $kk=>$vv){
                   if($vv['type']==$v['id']){
                       $vv['name'] = "".$vv['name'];
                       $nav[$i] = $vv;
                       $i++;
                       foreach ($data as $kkk=>$vvv){
                           if($vvv['type']==$vv['id']){
                               $vvv['name'] = "".$vvv['name'];
                               $nav[$i] = $vvv;
                               $i++;
                           }
                       }
                   }
               }
            }
        }
        $this->assign('data',$nav);
        return $this->fetch();
    }
    /**
     * 添加
     * */
    public function add(){
        if(!Request::instance()->post()){
            $this->assign('navid',input('id'));
            return $this->fetch();
        }else{
            if(!input('navid')){
                $data = [
                    'name'=>input('name'),
                    'type'=>0,
                    'url'=>input('url'),
                    'create_time'=>time(),
                    'rank'=>1
                ];
            }else{
                $nav = $this->_Nav->getOne(input('navid'));
                if($nav['rank']==1){
                    $data = [
                        'name'=>input('name'),
                        'type'=>$nav['id'],
                        'url'=>input('url'),
                        'create_time'=>time(),
                        'rank'=>2
                    ];
                }elseif ($nav['rank']==2){
                    $data = [
                        'name'=>input('name'),
                        'type'=>$nav['id'],
                        'url'=>input('url'),
                        'create_time'=>time(),
                        'rank'=>3
                    ];
                }
            }
            $re = $this->_Nav->add($data);
            if($re){
                return json_echo_msg(200,'添加成功');
            }else{
                return json_echo_msg(500,'添加失败');
            }
        }
    }
    /**
     * 修改
     * */
    public function up(){
        if(!Request::instance()->post()){
            $data = $this->_Nav->getOne(input('id'));
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $data = [
                'name'=>input('name'),
                'url'=>input('url'),
            ];
            $re = $this->_Nav->up(['id'=>input('id')],$data);
            if($re){
                return json_echo_msg(200,'修改成功');
            }else{
                return json_echo_msg(500,'修改失败');
            }
        }
    }
    /**
     * 排序
     * */
    public function sort(){
        $data = ['sort'=>input('sort')];
        $re = $this->_Nav->up(['id'=>input('id')],$data);
        if($re){
            return json_echo_msg(200,'成功！');
        }else{
            return json_echo_msg(400,'失败！');
        }
    }
    /**
     * 获取一条数据
     * */
    public function getOne(){
        $data = $this->_Nav->getOne(input('id'));
        echo json_echo_msg(200,'成功',$data);
    }
    /**
     * 删除
     * */
    public function del(){
        $re = $this->_Nav->del(['id'=>input('id')]);
        if($re){
            return json_echo_msg(200,'删除成功');
        }else{
            return json_echo_msg(500,'删除失败');
        }
    }


}